package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if cfg.controllerLombokModel!false>
import lombok.AllArgsConstructor;
</#if>
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.bean.R;
import com.fansl.koala.quickdev.common.constants.CommonConstants;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import ${package.Entity?replace(".entity",".vo")}.${entity}Vo;
import ${package.Entity?replace(".entity",".bean.dto")}.${entity}DTO;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
<#if cfg.controllerLombokModel!false>
@AllArgsConstructor
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    <#assign serviceName='${table.serviceName?substring(1)?uncap_first}' />
    <#assign entityName='${entity?uncap_first}' />
    private final ${table.serviceName} ${serviceName};

    /**
    * 添加
    * @param ${entityName}DTO 参数
    * @return true/false
    */
    @PostMapping("")
    public Mono<R<Boolean>> add(@RequestBody ${entity}DTO ${entityName}DTO) {
        ${entity} ${entityName} = ${entityName}DTO2Bean(${entityName}DTO);
        boolean b = ${serviceName}.save(${entityName});
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键更新
    * @param ${entityName}DTO 参数
    * @return true/false
    */
    @PutMapping("")
    public Mono<R<Boolean>> updateById(@RequestBody ${entity}DTO ${entityName}DTO) {
        ${entity} ${entityName} = ${entityName}DTO2Bean(${entityName}DTO);
        boolean b = ${serviceName}.updateById(${entityName});
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键获取信息
    * @param id 主键
    * @return 查询数据
    */
    @GetMapping("/{id}")
    public Mono<R<${entity}Vo>> getById(@PathVariable Long id) {
        ${entity} ${entityName} = ${serviceName}.getById(id);
        ${entity}Vo ${entityName}Vo = ${entityName}2Vo(${entityName});
        return Mono.just(R.ok(${entityName}Vo));
    }

    /**
    * 根据主键删除
    * @param id 主键
    * @return true/false
    */
    @DeleteMapping("/{id}")
    public Mono<R<Boolean>> delById(@PathVariable Long id) {
       boolean b = ${serviceName}.removeById(id);
       return Mono.just(R.ok(b));
    }

    /**
    * 分页查询
    * @param page 分页询参数
    * @param ${entityName} 查询参数
    * @return 查询数据
    */
    @GetMapping("/page")
    public Mono<R<PageBean<${entity}Vo>>> page(PageBean<${entity}Vo> page, ${entity} ${entityName}) {
        //查询
        PageBean<${entity}Vo> result = ${serviceName}.listPage(page,${entityName});
        return Mono.just(R.ok(result));
    }

    /**
    * Vo对象转换
    * @param ${entityName} 源对象
    * @return 目标对象
    */
    private ${entity}Vo ${entityName}2Vo(${entity} ${entityName}){
        ${entity}Vo target = new ${entity}Vo();
        BeanUtil.copyProperties(${entityName},target);
        return target;
    }

    /**
    * DTO对象转换
    * @param ${entityName}DTO 源对象
    * @return 目标对象
    */
    private ${entity} ${entityName}DTO2Bean(${entity}DTO ${entityName}DTO){
        ${entity} target = new ${entity}();
        BeanUtil.copyProperties(${entityName}DTO,target);
        return target;
    }
}
</#if>
